import 'package:flutter/cupertino.dart';
import 'package:flutter_cupertino_datetime_picker/flutter_cupertino_datetime_picker.dart';
import 'package:hitutor/Util/DateUtil.dart';
import 'package:hitutor/Util/Global.dart';
import 'package:hitutor/Util/HttpGo.dart';
import 'package:hitutor/Util/MyColor.dart';
import 'package:hitutor/Util/PageUtil.dart';
import 'package:hitutor/Widget/ChooserBox.dart';
import 'package:hitutor/Widget/MyBgPage.dart';
import 'package:hitutor/Widget/MyButton.dart';
import 'package:hitutor/Widget/MyDialog.dart';
import 'package:hitutor/Widget/MyImage.dart';
import 'package:hitutor/Widget/MyPage.dart';

import 'PreviewClass.dart';

class PreviewList extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _PreviewList();
  }
}

class _PreviewList extends State<PreviewList> {
  // DateTime? startTime;
  // DateTime? endTime;
  //
  // ChooseStartTime() {
  //   DatePicker.showDatePicker(context,
  //       dateFormat: "yyyy年 MM月 dd日 HH時 mm分", //通过空格设置滚轮数量
  //       pickerTheme: DateTimePickerTheme(confirm: Text("完成", style: TextStyle(fontSize: 14, color: MyColor.C58))),
  //       onConfirm: (DateTime dateTime, List<int> selectedIndex) {
  //     if (endTime != null && dateTime.isAfter(endTime!)) {
  //       showToast("開始時間必須早於結束時間");
  //       return;
  //     }
  //     startTime = dateTime;
  //     chooseClass = null;
  //     getClass();
  //     setState(() {});
  //   });
  // }
  //
  // ChooseEndTime() {
  //   DatePicker.showDatePicker(context,
  //       dateFormat: "yyyy年 MM月 dd日 HH時 mm分", //通过空格设置滚轮数量
  //       pickerTheme: DateTimePickerTheme(confirm: Text("完成", style: TextStyle(fontSize: 14, color: MyColor.C58))),
  //       onConfirm: (DateTime dateTime, List<int> selectedIndex) {
  //     if (startTime != null && dateTime.isBefore(startTime!)) {
  //       showToast("開始時間必須早於結束時間");
  //       return;
  //     }
  //     endTime = dateTime;
  //     chooseClass = null;
  //     getClass();
  //     setState(() {});
  //   });
  // }

  dynamic chooseClass;
  List<dynamic> classList = [];

  ChooseClass() {
    // if (startTime == null || endTime == null) {
    //   showToast("需要先選擇開始與結束日期");
    //   return;
    // }
    showWheelDialog2(context, "", classList, "displayLessonName", (int index) {
      chooseClass = classList[index];
      setState(() {});
      getUserClass(chooseClass["lessonId"].toString());
    });
  }

  @override
  void initState() {
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
      getClass();
      getUserClass("");
    });
    super.initState();
  }

  getClass() {
    // if (startTime == null || endTime == null) {
    //   return;
    // }
    // "beginDateTime": getUtcTime(startTime!), "endDateTime": getUtcTime(endTime!)
    HttpGo(context).post("/Material/GetPreviewMaterialLesson", {"studentId": Global.studentId}, (data) {
      classList = data["lessonList"];
    }, (code, error) => showToast(error));
  }

  List<dynamic> userClassList = [];

  getUserClass(String lessonId) {
    Map<String, dynamic> params = {
      "studentId": Global.studentId,
      "limit": 10,
    };
    // if (startTime != null) {
    //   params["beginDateTime"] = getUtcTime(startTime!);
    // }
    // if (endTime != null) {
    //   params["endDateTime"] = getUtcTime(endTime!);
    // }
    if (lessonId.isNotEmpty) {
      params["lessonId"] = lessonId;
    }
    HttpGo(context).post("/Material/GetPreviewMaterialClassroom/", params, (data) {
      userClassList = data["lessonList"];
      setState(() {});
    }, (code, error) => showToast(error));
  }

  @override
  Widget build(BuildContext context) {
    return MyBgPage(
        "",
        Container(
            child: Column(
          children: [
            Padding(
                padding: EdgeInsets.fromLTRB(12, 12, 12, 0),
                child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                  // Text("選擇開始與結束日期", style: TextStyle(fontSize: 13, fontWeight: FontWeight.bold, color: MyColor.Green)),
                  // Padding(
                  //     padding: EdgeInsets.fromLTRB(0, 8, 0, 8),
                  //     child: Row(children: [
                  //       Expanded(child: ChooserBox(getStrTime(startTime), bgColor: MyColor.White, onTap: ChooseStartTime)),
                  //       Text("　~　"),
                  //       Expanded(child: ChooserBox(getStrTime(endTime), bgColor: MyColor.White, onTap: ChooseEndTime)),
                  //     ])),
                  Text("選擇課程", style: TextStyle(fontSize: 13, fontWeight: FontWeight.bold, color: MyColor.Green)),
                  ChooserBox(chooseClass == null ? "" : chooseClass["displayLessonName"],
                      bgColor: MyColor.White, margin: EdgeInsets.fromLTRB(0, 8, 0, 8), onTap: ChooseClass),
                ])),
            Expanded(
                child: ListView.builder(
              itemCount: userClassList.length,
              itemBuilder: (BuildContext context, int index) {
                return ItemClass(userClassList[index]);
              },
            ))
          ],
        )),
        bgColor: MyColor.F3);
  }
}

class ItemClass extends StatelessWidget {
  dynamic data;

  ItemClass(this.data);

  @override
  Widget build(BuildContext context) {
    String beginTime = getStrTime(getTimeByUtc(data["beginTime"]), DateUtil.YMDHM);
    String endTime = getStrTime(getTimeByUtc(data["endTime"]), DateUtil.HM);

    String className = data["lessonName"].toString();
    if (className.length > 10) {
      className = className.substring(0, 10) + "...";
    }

    return GestureDetector(
        onTap: () {
          PageUtil().open(context, PreviewClass(data));
        },
        child: Container(
            color: MyColor.White,
            margin: EdgeInsets.only(bottom: 8),
            padding: EdgeInsets.all(12),
            child: Row(children: [
              Container(
                  decoration: Global.getBg(MyColor.F3, 24),
                  child: NetImage(data["teacherPhoto"], width: 32, height: 32,radius: 16, padding: EdgeInsets.all(4))),
              Container(width: 16),
              Expanded(
                  child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                Row(children: [
                  Text("${data["teacherName"]}", style: TextStyle(fontSize: 14, color: Color(0xffff8484), fontWeight: FontWeight.bold)),
                  Container(width: 8),
                  data["teacherWebcam"] ? Container() : LocalImage("video-off.png", width: 20)
                ]),
                Button("${data["plateformShortName"]}",
                    height: 20, width: 20, bgColor: MyColor.C5, radius: 20, fontSize: 11, margin: EdgeInsets.only(top: 4))
              ])),
              Text("$className\n$beginTime - $endTime",
                  textAlign: TextAlign.end, style: TextStyle(fontSize: 13, height: 2, color: Color(0xff005eab)))
            ])));
  }
}
